/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panels;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JOptionPane;

import src.*;

/**
 *
 * @author JMix
 */
public class VistaDatosRamo extends javax.swing.JDialog {

    private boolean aceptado;
    private boolean agregar;
    private String nombreEditando;
    /**
     * Creates new form VistaDatosRamo
     */
    public VistaDatosRamo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.jPanelRamo1.setPadre(this);
        super.setLocationRelativeTo(parent);
        nombreEditando = "";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelRamo1 = new panels.JPanelRamo();
        jPanelRequisitos1 = new panels.JPanelRequisitos();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTabbedPane1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTabbedPane1FocusGained(evt);
            }
        });
        jTabbedPane1.addTab("Ramo", jPanelRamo1);
        jTabbedPane1.addTab("Pre requisitos", jPanelRequisitos1);

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 577, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        Component selectedComponent = this.jTabbedPane1.getSelectedComponent();
        if (!this.jPanelRamo1.formularioSinLlenar()){   
                Ramo ramo = this.jPanelRamo1.getRamo();
                 if (ramo != null){
                    if(this.agregar && existeCurso(ramo.getNivel(), ramo.getNombre()) && !nombreEditando.trim().equals(ramo.getNombre().trim())){
                        JOptionPane.showMessageDialog(this, "Ya existe un ramo con el nombre \"" + ramo.getNombre()+"\".  ", this.getTitle(), JOptionPane.ERROR_MESSAGE);
                    } else {
                        this.jPanelRequisitos1.agregarRamo(ramo);
                        super.setVisible(false);
                        this.aceptado = true; 
                    }      
                }
        }
        else{
            JOptionPane.showMessageDialog(this, "Los campos con (*) son obligatorios.", this.getTitle(), JOptionPane.ERROR_MESSAGE);
            this.jPanelRamo1.getjLabel12().setForeground(Color.red);
            this.jPanelRamo1.getjLabel15().setForeground(Color.red);
            
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        super.setVisible(false);
        this.aceptado = false;
        this.agregar = false;
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        super.setVisible(false);
        this.aceptado = false;
        this.agregar = false;
    }//GEN-LAST:event_formWindowClosing

    private void jTabbedPane1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTabbedPane1FocusGained

    }//GEN-LAST:event_jTabbedPane1FocusGained

    public void btnAceptar(){
        this.btnAceptarActionPerformed(null);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VistaDatosRamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VistaDatosRamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VistaDatosRamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VistaDatosRamo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VistaDatosRamo dialog = new VistaDatosRamo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private panels.JPanelRamo jPanelRamo1;
    private panels.JPanelRequisitos jPanelRequisitos1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    public Ramo getRamo() {
        if (this.aceptado){
            Ramo ramo = this.jPanelRamo1.getRamo();
            ArrayList<Ramo> preRequisitos = this.jPanelRequisitos1.obtenerPreRequisitos();
            if (preRequisitos.size() > 0){
                for (Ramo r : preRequisitos){
                    ramo.agregarRequisito(r);
                }        
            }
            return ramo;
            }
        return null;
    }
    
    public ArrayList<String> getNombresRequisitos(){
        return this.jPanelRequisitos1.getRequisitos();
    }
    
    public void reset(int cantidadSemestres) {
        this.jPanelRamo1.reset(cantidadSemestres);
        this.jPanelRequisitos1.reset();
    }

    public void mostrarInformacion(Ramo ramo) {
        this.jPanelRamo1.mostrarInfoRamo(ramo);
        this.jPanelRequisitos1.mostrarPreRequisitos(ramo);
    }

    public void cargarBoxNiveles(int cantidadSemestres) {
        this.jPanelRamo1.cargarNiveles(cantidadSemestres);
    }

    public boolean getAceptado() {
        return this.aceptado;
    }

    void cargarRamosAnteriores(int i) {
        jPanelRequisitos1.cargarRamosAnteriores(i);
    }
    
    public void setMalla(Malla malla){
        this.jPanelRequisitos1.setMalla(malla);
    }

    public void setAceptado(boolean b) {
        this.aceptado = false;
    }
    
    @Override
    public void setVisible(boolean b){
        this.jTabbedPane1.setSelectedIndex(0);
        super.setVisible(b);
    }
    
    public boolean existeCurso(int nivel, String nombre){
        return this.jPanelRequisitos1.existeCurso(nivel, nombre);
    }
    
    public void setAgregar(boolean b){
        this.agregar = b;
    }

    void limpiarRequisitosSeleccionados() {
        this.jPanelRequisitos1.modeloListaRequisitosRamo.removeAllElements();
    }

    void cargarRamosAnteriores(int nivel, String tipo) {
        this.jPanelRequisitos1.cargarPosiblesPreRequisitos(nivel, tipo);
    }

    public JPanelRamo getjPanelRamo1() {
        return jPanelRamo1;
    }

    public JPanelRequisitos getjPanelRequisitos1() {
        return jPanelRequisitos1;
    }

    public String getNombreEditando() {
        return nombreEditando;
    }

    public void setNombreEditando(String nombreEditando) {
        this.nombreEditando = nombreEditando;
    }
    
    
}
